<template>
	<view class="content">
		<view class="topbar">
			<view class="back" @tap="back()">
				<image src="../../static/image/search/back.png" mode=""></image>
			</view>
			<view class="searchbox">
				<input type="text" v-model="keywords" placeholder="请输入关键词" />
			</view>
			<view class="search" @tap="search()">
				<image src="../../static/image/search/search.png" mode=""></image>
			</view>
		</view>
		<view class="songlist">
			<view class="card" v-for="(item,index) in songslist" :key='index'>
				<view class="title">
					<view class="img">
						<view class="box">
							<image :src="item.picurl" mode=""></image>
						</view>
					</view>
					<view class="songname">
						{{item.songname}}
					</view>
					<view class="add">
						<image src="../../static/image/search/add.png" mode=""></image>
					</view>
					<view class="play">
						<image src="../../static/image/search/play.png" mode=""></image>
					</view>
				</view>
				<view class="msg">
					<view class="singer">
						{{item.artistname}}
					</view>
					<view class="time">
						{{item.time + ' min'}}
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import request from '../../utils/request.js'
	export default {
		data() {
			return {
				keywords: '',
				songslist: [],
			};
		},
		methods: {
			//搜索歌曲
			search: function() {
				this.songslist = [];
				if (this.keywords) {
					let opts = {
						url: '/search',
						data: {
							keywords: this.keywords,
						},
						method: 'GET',
					}
					request.request(opts).then((res) => {
						res.result.songs.forEach((item, index) => {
							let id = item.id;
							let songname = item.name;
							let artistname = item.artists[0].name;
							let time = (item.duration / 1000 / 60).toFixed(2);
							let picurl = item.artists[0].img1v1Url;
							// let opts = {
							// 	url:'/artists',
							// 	data:{
							// 		id: item.artists[0].id
							// 	},
							// 	method:'GET'
							// };
							// request.request(opts).then(res=>{
							// 	console.log(res)
							// 	// picurl = res.artists[0].picUrl;
							// });
							this.songslist.push({
								id,
								songname,
								artistname,
								time,
								picurl,
							});
						});
					});
					// console.log(this.songslist);
				} else {
					uni.showToast({
						icon: 'none',
						title: '内容不能为空！',
						duration: 2000,
					})
				}
				// console.log(this.songslist)
			},
			
			//跳转到主页
			back: function (){
				uni.navigateTo({
					url:'../index/index'
				})
			}
		}
	}
</script>

<style lang="scss">
	.content {
		width: 100%;
		height: 100%;
		box-sizing: border-box;
		padding: $uni-spacing-row-sm 10rpx;
		// padding-top: var(--status-bar-height);
		// bottom: env(safe-area-inset-bottom);

		.topbar {
			display: flex;
			width: 100%;
			height: 72rpx;
			padding-bottom: 20rpx;
			border-bottom: 1rpx solid #F1F1F1;

			image {
				width: 60rpx;
				height: 60rpx;
			}

			.back {
				felx: 1;
				display: flex;
				justify-content: center;
				align-items: center;
				text-align: center;
			}

			.searchbox {
				flex: 7;
				color: #666;
				margin: 0 8rpx;

				input {
					width: 100%;
					height: 72rpx;
					text-align: center;
					border: 1rpx solid #C0C0C0;
					color: #666;
					border-radius: 12rpx;
				}
			}

			.search {
				flex: 1;
				display: flex;
				justify-content: center;
				align-items: center;
				text-align: center;
			}
		}

		.songlist {
			width: 100%;
			padding: $uni-spacing-row-base;
			box-sizing: border-box;
			display: flex;
			flex-direction:column;
			justify-content: center;
			align-items: center;

			.card {
				position: relative;
				width: 95%;
				height: 160rpx;
				margin-bottom: 50rpx;
				border-radius: 24rpx;
				padding: 20rpx 20rpx 0rpx 20rpx;
				box-shadow: 10rpx 10rpx 16rpx #808080;
				overflow: hidden;

				.title {
					display: flex;

					.img {
						flex: 2;
						.box {
							margin-left: 20rpx;
							width: 130rpx;
							height: 130rpx;
							border-radius: 12rpx;
							overflow: hidden;
							box-shadow: 1rpx 1rpx 20rpx #C0C0C0;
							image {
								width: 130rpx;
								height: 130rpx;
							}
						}
					}
					.songname {
						padding-left: 20rpx;
						flex: 4;
						line-height: 100rpx;
						font-size: 40rpx;
						font-weight: 550;
						color: #666;
						display: -webkit-box !important;
						text-overflow: ellipsis;
						overflow: hidden;
						-webkit-line-clamp: 1;
						-webkit-box-orient: vertical;
					}

					.add {
						flex: 1;
						padding-top: 24rpx;

						image {
							width: 60rpx;
							height: 60rpx;
						}
					}

					.play {
						flex: 1;
						padding-top: 24rpx;

						image {
							width: 60rpx;
							height: 60rpx;
						}
					}
				}

				.msg {
					position: relative;
					padding-top: -20rpx;
					.singer{
						position: absolute;
						top: -36rpx;
						margin-left: 180rpx;
						font-size: 32rpx;
						color: #808080;
						display: -webkit-box !important;
						text-overflow: ellipsis;
						overflow: hidden;
						-webkit-line-clamp: 1;
						-webkit-box-orient: vertical;
					}
					.time{
						position: absolute;
						top: -36rpx;
						right: 38rpx;
						font-size: 32rpx;
						color: #808080;
						display: -webkit-box !important;
						text-overflow: ellipsis;
						overflow: hidden;
						-webkit-line-clamp: 1;
						-webkit-box-orient: vertical;
					}
				}
			}
		}
	}
</style>
